package com.egame.manage.biz.web.controller.gc;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.egame.interfaces.EnumType.GameType;
import cn.egame.interfaces.gc.TerminalInfo;
import cn.egame.interfaces.gc.TerminalRecognitionInfo;
import cn.egame.interfaces.pu.AppParameter;
import com.egame.manage.biz.server.ManageClient;
import com.egame.manage.biz.server.cu.ManufacturerServiceImpl;
import com.egame.manage.biz.server.gc.TerminalRecognitionServiceImpl;
import com.egame.manage.biz.server.gc.TerminalServiceImpl;
import com.egame.manage.biz.web.controller.BaseController;
import com.egame.manage.biz.domain.TerminalBean;
import com.egame.manage.biz.utils.ConstVar;
import com.lenovo.manager.business.domain.UserBean;

import cn.egame.common.model.PageData;
import cn.egame.common.servlet.WebUtils;

/** 
 * 
 * @Copyright 爱游戏
 * 
 * @Project egame.manage
 * 
 * @Author Administrator
 * 
 * @timer 2013-2-22
 * 
 * @Version 1.0.0
 * 
 * @JDK version used 6.0
 * 
 * @Modification history none
 * 
 * @Modified by none
 */

@Controller
@RequestMapping("/tv/terminal")
public class TerminalController extends BaseController {
	public TerminalController(){		
	}
	public TerminalController(HttpServletRequest req, HttpServletResponse resp) {
		super(req, resp);
	}

	@RequestMapping(value = "/list", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView terminalList(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int page = WebUtils.getInt(req, "page",0);
		int rowsOfPage = WebUtils.getInt(req, "rowsOfPage",15);		
		int manufactureId = WebUtils.getInt(req, "manufacturerid", 0);
		String shortUa = WebUtils.getString(req, "shortUa","");
		String uacode = WebUtils.getString(req, "uacode","");
		String screenpx = WebUtils.getString(req, "screenpx", "");
		TerminalInfo beanInfo = new TerminalInfo();
		beanInfo.setManufacturerId(manufactureId);
		beanInfo.setShortUA(shortUa);
		beanInfo.setUACode(uacode);
		beanInfo.setScreenPx(screenpx);
		
		PageData pd = TerminalServiceImpl.getInstance().pageTerminalIds(appId, loginUserId, beanInfo, page, rowsOfPage);
		ModelAndView mav = new ModelAndView("/new/op/terminalList");
		mav.addObject("manuId",manufactureId);
		mav.addObject("scrPx",screenpx);
		mav.addObject("shortUa",shortUa);
		mav.addObject("uacode", uacode);
		mav.addObject("pd", pd);
		mav.addObject("pdUrl", "/tv/terminal/list");
		
		List<Integer> ids = ManageClient.getInstance().listAppParameterIdsByParentId(rowsOfPage, loginUserId, ConstVar.APP_SCREEN_PX);
		List<AppParameter> screenpxList = ManageClient.getInstance().listAppParameterByIds(page, loginUserId, ids);
		mav.addObject("screenpxList", screenpxList);
		
		List<String[]> brandList = ManufacturerServiceImpl.getInstance().listManufacturerBand(appId, loginUserId);
		mav.addObject("brandList", brandList);
		
		mav.addObject("shortua", shortUa);
		mav.addObject("uacode", uacode);
		
		return mav;
	}	

	@RequestMapping(value = "/set/show", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView showNewPage(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		ModelAndView mav = new ModelAndView("/new/op/newTerminal");		
		List<String[]> brandList = ManufacturerServiceImpl.getInstance().listManufacturerBand(appId, loginUserId);
		mav.addObject("brandList", brandList);
		
		List<Integer> osListIds = ManageClient.getInstance().listAppParameterIdsByParentId(appId, loginUserId, ConstVar.APP_OS);
		List<AppParameter> osList = ManageClient.getInstance().listAppParameterByIds(appId, loginUserId, osListIds);
		mav.addObject("osList", osList);
		
		List<Integer> pxListIds = ManageClient.getInstance().listAppParameterIdsByParentId(appId, loginUserId, ConstVar.APP_SCREEN_PX);
		List<AppParameter> pxList = ManageClient.getInstance().listAppParameterByIds(appId, loginUserId, pxListIds);
		mav.addObject("pxList", pxList);
		
		return mav;
	}
	@RequestMapping(value = "/set", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView setTerminal(TerminalInfo bean,HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		TerminalServiceImpl.getInstance().setTerminalInfo(appId, loginUserId, bean);		
		return terminalList(req,resp);
	}
	
	@RequestMapping(value = "/update", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView updateTerminal(TerminalBean bean,HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}		
		TerminalServiceImpl.getInstance().updateTerminalBean(appId, loginUserId, bean);
		return terminalList(req,resp);
	}

	@RequestMapping(value = "/edit", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView editTerminal(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int terminalId = WebUtils.getInt(req, "terminalId",0);
		TerminalInfo bean = ManageClient.getInstance().getTerminalInfoById(appId, loginUserId, terminalId);
		
		ModelAndView mav = new ModelAndView("/new/op/editTerminal");
		mav.addObject("bean", bean);
		System.out.println(bean.toString());
		
		List<String[]> brandList = ManufacturerServiceImpl.getInstance().listManufacturerBand(appId, loginUserId);
		mav.addObject("brandList", brandList);
		
		List<Integer> osListIds = ManageClient.getInstance().listAppParameterIdsByParentId(appId, loginUserId, ConstVar.APP_OS);
		List<AppParameter> osList = ManageClient.getInstance().listAppParameterByIds(appId, loginUserId, osListIds);
		mav.addObject("osList", osList);
		
		List<Integer> pxListIds = ManageClient.getInstance().listAppParameterIdsByParentId(appId, loginUserId, ConstVar.APP_SCREEN_PX);
		List<AppParameter> pxList = ManageClient.getInstance().listAppParameterByIds(appId, loginUserId, pxListIds);
		mav.addObject("pxList", pxList);
		return mav;
	}
	
	@RequestMapping(value = "/delete", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView deleteTerminal(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int terminalId = WebUtils.getInt(req, "terminalId",0);
		TerminalServiceImpl.getInstance().deleteTerminalInfo(appId, loginUserId, terminalId);
		TerminalRecognitionServiceImpl.getInstance().deleteTerminalRecognitionInfo(terminalId);
		return terminalList(req,resp);
	}
	
	@RequestMapping(value = "/list/recognition", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView pageTerminalRecognition(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int page = WebUtils.getInt(req,"page",0);
		int rowsOfPage = WebUtils.getInt(req,"rowsOfPage",15);		
		int manufactureId = WebUtils.getInt(req, "selectManufacturerId", 0);
		int terminalId = WebUtils.getInt(req, "selectTerminalId", 0);
		TerminalRecognitionInfo bean = new TerminalRecognitionInfo();
		bean.setTerminalId(terminalId);
		
		PageData pd = TerminalRecognitionServiceImpl.getInstance().pageTerminalRecognition(appId, loginUserId, page, rowsOfPage, bean, manufactureId);
		ModelAndView mav = new ModelAndView("/new/op/recognitionList");
		mav.addObject("pd", pd);
		mav.addObject("pdUrl", "/tv/terminal/list/recognition");		

		List<String[]> brandList = ManufacturerServiceImpl.getInstance().listManufacturerBand(appId, loginUserId);
		mav.addObject("brandList", brandList);	
		
		List<TerminalInfo> list = TerminalServiceImpl.getInstance().listTerminalInfo(appId, loginUserId);
		mav.addObject("terminals", list);
		
		mav.addObject("selectManufacture", manufactureId);
		mav.addObject("selectTerminal", terminalId);
		
		return mav;
	}

	@RequestMapping(value = "/set/recognition/show", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView showNewRecognition(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		List<TerminalInfo> list = TerminalServiceImpl.getInstance().listTerminalInfo(appId, loginUserId);
		List<String> terminalNames = TerminalServiceImpl.getInstance().listTerminalNames();
		List<String> terminalOs = TerminalServiceImpl.getInstance().listOsNames();
		List<String> terminalScreens = TerminalServiceImpl.getInstance().listScreenPxs();
 		ModelAndView mav = new ModelAndView("/new/op/newRecognition");
		mav.addObject("terminalNames", terminalNames);
		mav.addObject("terminalOs", terminalOs);
		mav.addObject("terminalScreens", terminalScreens);
		mav.addObject("list", list);
		
		return mav;
	}

	@ResponseBody
	@RequestMapping(value = "/identify", method = {RequestMethod.GET,RequestMethod.POST})
	public Map<String,Object> idengtifyModel(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		String popOS = WebUtils.getString(req, "popOS","");
		String popScreenPX = WebUtils.getString(req, "popScreenPX","");
		String popManufacturer = WebUtils.getString(req, "popManufacturer","");
		String popModelName = WebUtils.getString(req, "popModelName","");
		return TerminalServiceImpl.getInstance().listTerminalIdAndShortUa(popManufacturer, popOS, popScreenPX, popModelName);
	}
	
	@RequestMapping(value = "/set/recognition", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView setTerminalRecogintion(TerminalRecognitionInfo bean,HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		bean.setGameType(GameType.tv);
		int terminalId = WebUtils.getInt(req, "terId", 0);
		bean.setTerminalId(terminalId);
		TerminalRecognitionServiceImpl.getInstance().setTerminalRecognitionInfo(appId, loginUserId, bean);
		return pageTerminalRecognition(req,resp);
	}	

	@RequestMapping(value = "/edit/recognition", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView editTerminalRecogintion(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		ModelAndView mav = new ModelAndView("/new/op/editTerminalRecognition");		
		String apiCode = WebUtils.getString(req, "apiCode","");
		String uaCode = WebUtils.getString(req, "uaCode","");
		String screenPx = WebUtils.getString(req, "screenPx","");
		List<String> terminalNames = TerminalServiceImpl.getInstance().listTerminalNames();
		List<String> terminalOs = TerminalServiceImpl.getInstance().listOsNames();
		List<String> terminalScreens = TerminalServiceImpl.getInstance().listScreenPxs();
		TerminalRecognitionInfo beanInfo = ManageClient.getInstance().getTerminalRecognitionInfo(appId, loginUserId, apiCode,uaCode,screenPx);
		mav.addObject("bean", beanInfo);
		List<TerminalInfo> list = TerminalServiceImpl.getInstance().listTerminalInfo(appId, loginUserId);
		mav.addObject("terminalNames", terminalNames);
		mav.addObject("terminalOs", terminalOs);
		mav.addObject("terminalScreens", terminalScreens);
		mav.addObject("list", list);
		
		return mav;
	}

	@RequestMapping(value = "/update/recognition", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView updateTerminalRecogintion(TerminalRecognitionInfo bean,HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int terminalId = WebUtils.getInt(req, "terId", 0);
		bean.setGameType(GameType.tv);
		bean.setTerminalId(terminalId);
		TerminalRecognitionServiceImpl.getInstance().updateTerminalRecognitionInfo(appId, loginUserId, bean);
		return pageTerminalRecognition(req,resp);
	}
	
	@RequestMapping(value = "/delete/recognition", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView deleteTerminalRecogintion(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		String apiCode = WebUtils.getString(req, "apiCode","");
		String uaCode = WebUtils.getString(req, "uaCode","");
		String screenPx = WebUtils.getString(req, "screenPx","");
		TerminalRecognitionServiceImpl.getInstance().deleteTerminalRecognitionInfo(apiCode,uaCode,screenPx);
		return pageTerminalRecognition(req,resp);
	}
	
	@ResponseBody
	@RequestMapping(value = "/exist", method = {RequestMethod.GET,RequestMethod.POST})
	public int isTerminalExist(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		String uacode = WebUtils.getString(req, "uacode","");
		if(TerminalServiceImpl.getInstance().isTerminalExist(uacode)){
			return 1;
		}
		return 0;
	}
	
}
